﻿
@{
    ViewBag.Title = "选择用户";
    Layout = "~/Views/Shared/_LayoutBase.cshtml";
}

@section styles{

}
<div id="dMain">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="ibox" style="margin-bottom: 0px;">
            <div class="col-md-5">
                @using (Ajax.BeginForm("_AddUser", "Components", new AjaxOptions() { HttpMethod = "post", OnSuccess = "afterOk" }, new { area = "", id = "datarightfrom", name = "datarightfrom" }))
                {
                    @Html.AntiForgeryToken()
                    <div id="toolbar">
                        <button type="button" class="btn btn-success" id="btn-check-all">全选</button>
                        <button type="button" class="btn btn-danger" id="btn-uncheck-all">全不选</button>
                    </div>
                    <div id="treeview-checkable" class="" style="margin-top:5px;"></div>
                    <input id="Right" type="hidden" value="@ViewBag.right" />
                    @*<div class="from-footer">
                        <div class="btn-wrap" style="position: fixed;">
                            <a class="btn btn-sm btn-primary radius" onclick="savefrom()">
                                <i class="iconfont icon-save"></i>
                                提交保存
                            </a>
                            <a class="btn btn-sm btn-warning radius" onclick="parent.layer.close(parent.layer.index);">
                                <i class="iconfont icon-fanhui"></i>
                                取消关闭
                            </a>
                        </div>
                    </div>*@
                }
            </div>
            <div class="col-md-7"></div>
        </div>
    </div>
</div>
@section scripts{
    <script type="text/javascript" charset="utf-8" src="@Url.Content("~/Content/JS/common/jquery/jquery.unobtrusive-ajax.min.js")"></script>
    <script type="text/javascript" charset="utf-8" src="@Url.Content("~/Scripts/MicrosoftAjax.js")"></script>
    <script type="text/javascript" charset="utf-8" src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")"></script>
    <script type="text/javascript" charset="utf-8" src="@Url.Content("~/Content/JS/common/bootstrap/bootstrap-treeview.min.js")"></script>
    <script>
        //function savefrom() {
        //    $("#datarightfrom").submit();
        //}
        //function afterOk(data) {
        //    parent.layer.close(parent.layer.index);
        //    var check = $('#treeview-checkable').treeview('getChecked');
        //    //$.each(check, function () {
        //    //    return $(this).nodeId;
        //    //});
        //    var textlist = new Array();
        //    var idlist = new Array();
        //    $.each(check, function (i,o) {
        //        idlist.push(o.id);
        //    });
        //    var idstr = idlist.join(',');

        //    $.each(check, function (i, o) {
        //        textlist.push(o.text);
        //    });
        //    var strs = textlist.join(',');
        //    //$.map(check, function () { return $(this).nodeId; }).get().join(",");
        //    //var checklist = [];
        //    //check.each(function (i) {
        //    //    checklist.join(',', check[i].nodeId);
        //    //})

        //    //var checklist = $.map(check,function () {
        //    //    return $(this).nodeId;
        //    //}).get().join(",");
        //    parent.getUserValue(idstr);
        //    parent.getUserText(strs);
        //}

        var callbackiddata = function () {
            var check = $('#treeview-checkable').treeview('getChecked');
            var idlist = new Array();
            $.each(check, function (i, o) {
                idlist.push(o.id);
            });
            var idstr = idlist.join(',');
            return idstr;
        }
        var callbacktextdata = function () {
            var check = $('#treeview-checkable').treeview('getChecked');
            var textlist = new Array();
            $.each(check, function (i, o) {
                textlist.push(o.text);
            });
            var strs = textlist.join(',');
            return strs;
        }
    $(function () {



        $.getJSON('@Url.Action("GetjsonTreeData", "Components", new { area = ""})', { idstr: $('#Right').val() },function (data) {
            if (data != null) {
                $('#treeview-checkable').treeview({
                    data: data,
                    showIcon: true,
                    showCheckbox: true,    
                    uncheckedIcon: "glyphicon glyphicon-unchecked", 
                    //multiSelect: true,// 不允许多选，因为我们要通过check框来控制
                    hierarchicalCheck: true,
                    onNodeChecked: nodeChecked,
                    onNodeUnchecked: nodeUnchecked

                });
            }
        })
        var nodeCheckedSilent = false;
        function nodeChecked(event, node) {
            if (nodeCheckedSilent) {
                return;
            }
            nodeCheckedSilent = true;
            checkAllParent(node);
            checkAllSon(node);
            nodeCheckedSilent = false;
        }

        var nodeUncheckedSilent = false;
        function nodeUnchecked(event, node) {
            if (nodeUncheckedSilent)
                return;
            nodeUncheckedSilent = true;
            uncheckAllParent(node);
            uncheckAllSon(node);
            nodeUncheckedSilent = false;
        }

        //选中全部父节点
        function checkAllParent(node) {
            $('#treeview-checkable').treeview('checkNode', node.nodeId, { silent: true });
            var parentNode = $('#treeview-checkable').treeview('getParent', node.nodeId);
            if (!("nodeId" in parentNode)) {
                return;
            } else {
                checkAllParent(parentNode);
            }
        }
        //取消全部父节点
        function uncheckAllParent(node) {
            $('#treeview-checkable').treeview('uncheckNode', node.nodeId, { silent: true });
            var siblings = $('#treeview-checkable').treeview('getSiblings', node.nodeId);
            var parentNode = $('#treeview-checkable').treeview('getParent', node.nodeId);
            if (!("nodeId" in parentNode)) {
                return;
            }
            var isAllUnchecked = true; //是否全部没选中
            for (var i in siblings) {
                if (siblings[i].state.checked) {
                    isAllUnchecked = false;
                    break;
                }
            }
            if (isAllUnchecked) {
                uncheckAllParent(parentNode);
            }

        }

        //级联选中所有子节点
        function checkAllSon(node) {
            $('#treeview-checkable').treeview('checkNode', node.nodeId, { silent: true });
            if (node.nodes != null && node.nodes.length > 0) {
                for (var i in node.nodes) {
                    checkAllSon(node.nodes[i]);
                }
            }
        }
        //级联取消所有子节点
        function uncheckAllSon(node) {
            $('#treeview-checkable').treeview('uncheckNode', node.nodeId, { silent: true });
            if (node.nodes != null && node.nodes.length > 0) {
                for (var i in node.nodes) {
                    uncheckAllSon(node.nodes[i]);
                }
            }
        }




        // Check/uncheck all
        $('#btn-check-all').on('click', function (e) {
            $('#treeview-checkable').treeview('checkAll', { silent: $('#chk-check-silent').is(':checked') });
        });

        $('#btn-uncheck-all').on('click', function (e) {
            $('#treeview-checkable').treeview('uncheckAll', { silent: $('#chk-check-silent').is(':checked') });
        });
        });

    </script>
}
